package com.itkjb.leisurely.resource.open.sys.controller;


import com.itkjb.leisurely.core.base.BaseResult;
import com.itkjb.leisurely.core.base.controller.BaseController;
import com.itkjb.leisurely.core.log.annotion.LeisurelyRequestLog;
import com.itkjb.leisurely.resource.sys.model.SysDepartmentModel;
import com.itkjb.leisurely.resource.sys.model.vo.SysDepartmentVO;
import com.itkjb.leisurely.resource.sys.service.ISysDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 组织结构 接口层
 * </p>
 *
 * @author itkjb.com
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/open/sys/sys-department")
@Api(value = "组织开放接口", tags = {"组织开放相关接口"})
public class SysDepartmentOpenController extends BaseController<SysDepartmentModel, ISysDepartmentService> {

    /**
     * 获取组织结构树
     * @param id
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取组织结构树", notes = "")
    @LeisurelyRequestLog(paramsName="id")
    public BaseResult<List<SysDepartmentModel>> tree(@ApiParam("指定id组织 下的树") @RequestParam(name = "id",required = false) String id) {
        BaseResult<List<SysDepartmentModel>> res = new BaseResult<>();
        // SearchModel<SysDepartmentModel> searchModel= QueryUtil.build(param,model);
        List<SysDepartmentVO> tree = this.baseService.tree(id);
        return res.toSuccess().setData(tree);

    }

    /**
     * 获取指定组织id的所有下级组织id，此接口返回的不包含自身id
     * @param id
     * @return
     */
    @GetMapping("/getSubIds/{id}")
    @ApiOperation(value = "获取指定组织id的所有下级组织id", notes = "此接口返回的不包含自身id")
    public BaseResult<Set<String>> getSubIds(@PathVariable("id") String id) {
        BaseResult<Set<String>> res = new BaseResult<>();
        if(StringUtils.isBlank(id)){
            res.setMsg("参数缺失");
            return res;
        }
        Set<String> subIds = this.baseService.getSubIds(id);
        return res.toSuccess().setData(subIds);
    }

    /**
     * 获取指定组织id的所有下级组织id，此接口返回的不包含自身id
     * @param id
     * @return
     */
    @GetMapping("/getIdAndSubIds/{id}")
    @ApiOperation(value = "获取指定组织id的所有下级组织id及自身id", notes = "此接口返回的包含自身id,因此肯定有一个值")
    public BaseResult<Set<String>> getIdAndSubIds(@PathVariable("id") String id) {
        BaseResult<Set<String>> res = new BaseResult<>();
        if(StringUtils.isBlank(id)){
            res.setMsg("参数缺失");
            return res;
        }
        Set<String> subIds = this.baseService.getIdAndSubIds(id);
        return res.toSuccess().setData(subIds);
    }

    /**
     * 判断指定的一个父类id是否可用
     * @param id 主体的id
     * @param parentId 指定父级id
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/parentIdIsEnable/{id}/{parentId}")
    @ApiOperation(value = "获取组织结构列表", notes = "")
    public BaseResult<Boolean> parentIdIsEnable(@PathVariable("id") String id, @PathVariable("parentId") String parentId,HttpServletRequest request, HttpServletResponse response) {
        BaseResult<Boolean> res = new BaseResult<>();
        if(StringUtils.isBlank(id) || StringUtils.isBlank(parentId)){
            return res.setMsg("参数缺失");
        }
        Set<String> subIds = this.baseService.getSubIds(id);
        // 判断指定的父级，是否是id对象的子集
        boolean isContains = false;
        if(subIds !=null && subIds.size()>0){
            isContains = subIds.contains(parentId);
        }
        if(!isContains){
            isContains = id.equals(parentId);
        }
        // 如果 指定的parentId 是id对象的子集，则不可用
        return res.toSuccess().setData(!isContains);
    }


}

